package com.hsnn.medstgmini.base.dict.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.DicExperttypeLogDao;
import com.hsnn.medstgmini.dao.impl.HibernateSqlUtilImpl;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 专家 类型日志
 * @author 王炳
 * @date 2015年7月13日
 */
@Repository
public class DicExperttypeLogDaoImpl implements DicExperttypeLogDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	@Autowired
	private HibernateSqlUtilImpl dao;
	
	/**
	 * @category 获取专家类型日志列表
	 * @date 2015年7月13日
	 * @return page
	 */
	@Override
	public Pagination getDicExperttypeLogList(Pagination page){
		Map<String,String> params = new HashMap<String, String>();
		String dicExperttypeId = page.getConditions().get("dicExperttypeId");
		String startDate = page.getConditions().get("startDate");
		String endDate = page.getConditions().get("endDate");
		StringBuffer sql = new StringBuffer();
		sql.append("	SELECT 																			")
		   .append("	a.status status,e.name updUser,a.log_id logId,a.detail detail,		    						")
		   .append("	DATE_FORMAT(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime						")
		   .append("	FROM dic_experttype_log a 														")
		   .append("	LEFT JOIN sys_user e ON e.id = a.upd_user										")
		   .append("	WHERE a.dic_experttype_id= :dicExperttypeId										");
		params.put("dicExperttypeId", dicExperttypeId);
		if(StringUtils.isNotBlank(startDate)){
			sql.append(" and a.upd_datetime >= :startDate  ");
			params.put("startDate", startDate+" 00:00:00");
		}
		if(StringUtils.isNotBlank(endDate)){
			sql.append(" and a.upd_datetime <= :endDate  ");
			params.put("endDate", endDate+" 23:59:59");
		}
		sql.append(" order by a.upd_datetime desc");
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}

	
	/**
	 * 基础库 数据 字典 专家类型 日志详情
	 * @param result
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDicExperttypeLogListDetail(Pagination page) {
		Session session = sessionFactory.getCurrentSession();
		String logId = page.getConditions().get("logId");
		StringBuffer  sql = new StringBuffer();
		sql.append("	SELECT 																			")
		   .append("	CASE WHEN a.expert_type_code LIKE '_________' THEN d.expert_type_name			")
		   .append("	WHEN a.expert_type_code LIKE '_______' THEN c.expert_type_name					")
		   .append("	WHEN a.expert_type_code LIKE '_____' THEN b.expert_type_name					")
		   .append("	WHEN a.expert_type_code LIKE '___' THEN a.expert_type_name						")
		   .append("	ELSE NULL END																	")
		   .append("	firstLevelName,																	")
		   .append("	CASE WHEN a.expert_type_code LIKE '_________' THEN c.expert_type_name			")
		   .append("	WHEN a.expert_type_code LIKE '_______' THEN b.expert_type_name					")
		   .append("	WHEN a.expert_type_code LIKE '_____' THEN a.expert_type_name					")
		   .append("	ELSE NULL END																	")
		   .append("	secoLevelName,																	")
		   .append("	CASE WHEN a.expert_type_code LIKE '_________' THEN b.expert_type_name			")
		   .append("	WHEN a.expert_type_code LIKE '_______' THEN a.expert_type_name					")
		   .append("	ELSE NULL END																	")
		   .append("	thirdLevelName,																	")
		   .append("	CASE WHEN a.expert_type_code LIKE '_________' THEN a.expert_type_name			")
		   .append("	ELSE NULL END																	")
		   .append("	forthLevelName,																	")
		   .append("	a.status status,e.name updUser,a.expert_type_name experttypeName,													")
		   .append("	DATE_FORMAT(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime						")
		   .append("	FROM dic_experttype_log a 														")
		   .append("	LEFT JOIN dic_experttype b ON a.father_experttype_id=b.dic_experttype_id	")
		   .append("	LEFT JOIN dic_experttype c ON b.father_experttype_id=c.dic_experttype_id	")
		   .append("	LEFT JOIN dic_experttype d ON c.father_experttype_id=d.dic_experttype_id	")
		   .append("	LEFT JOIN sys_user e ON e.id = a.upd_user										")
		   .append(" WHERE a.dic_experttype_id = (SELECT dic_experttype_id FROM dic_experttype_log WHERE log_id = :logId) ")
		   .append(" AND a.upd_datetime <= (SELECT upd_datetime FROM dic_experttype_log WHERE log_id = :logId) ")
		   .append(" order by a.upd_datetime desc limit 2 ");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("logId", logId);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	}
	
	
}	
